//
//  YHPStatusPictureView.swift
//  SinaSwift
//
//  Created by Yan on 16/3/15.
//  Copyright © 2016年 baidu. All rights reserved.
//

import UIKit


class YHPStatusPictureView: UICollectionView {
    private let pictureCellId = "pictureCellId"
    
    
    init () {
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = pictureMargin
        layout.minimumLineSpacing = pictureMargin
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        self.registerClass(PictureCell.self, forCellWithReuseIdentifier: pictureCellId)
        self.dataSource = self
        // 设置配图视图背景色为白色
        self.backgroundColor = UIColor.whiteColor()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //集合视图显示图片数据源 + 获取图片的数量
    var imageURLs: [NSURL]? {
        didSet {
            //1.根据配图的数量 计算配图视图的大小
            let pSize = calculatePictureViewSize()
            //更新约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pSize)
            }
            //2.刷新集合视图
            self.reloadData()
        }
    }

    private func calculatePictureViewSize() -> CGSize {
        let imageCount = imageURLs?.count ?? 0
        let maxWidth = ScreenWidth - 2 * commonMargin
        let itemWidth = (maxWidth - 2 * pictureMargin) / 3
        
        //如何获取layout对象
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //设置itemSize
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        if imageCount == 0 {
            return CGSizeZero
        }
    
        if imageCount == 1 {
            //给定图片远尺寸大小
            //TODO:  比较困难 后续完善 线给定 固定大小
            let imageSize = CGSizeMake(100, 100)
            //TODO: 需要设置 itmeSize
            layout.itemSize = imageSize
            return imageSize
        }
        
        if imageCount == 4 {
            //计算配图大小  2 * itemSize.width + margin
            let w = itemWidth * 2 + pictureMargin
            return CGSizeMake(w, w)
        }
        
        //其他多张图片
        //确定行数  根据图片数量确定有多少行
        /*
        1,2,3   -> 1
        4,5,6   -> 2
        7,8,9   -> 3
        */
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let pHeight = row * itemWidth + (row - 1) * pictureMargin
        
        
        return CGSizeMake(maxWidth, pHeight)
    }
    //MARK: 设置UI界面
    private func setupUI() {
        addSubview(testLabel)
        testLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.center)
        }
    }
    
    private lazy var testLabel: UILabel = UIFactory.factoryLabelWith("", textColor: commonColor, fontSize: 40)

}


//数据源协议
extension YHPStatusPictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! PictureCell
        
        //        //设置测试颜色
        //        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.greenColor()
        
        //绑定数据
        cell.imageURL = imageURLs![indexPath.item]
        return cell
    }
}

class PictureCell: UICollectionViewCell {
    
    var imageURL: NSURL? {
        didSet {
            iconView.sd_setImageWithURL(imageURL)
        }
    }

    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //默认实现报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 设置UI界面
    private func setupUI() {
        contentView.addSubview(iconView)
        //设置约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    }

    //MARK: 懒加载子视图
    private lazy var iconView: UIImageView = {
       let imageView = UIImageView()
        imageView.clipsToBounds = true
        return imageView
    }()
}
